package com.liming.product.controller;

import com.liming.common.pojo.dto.product.AddProductDTO;
import com.liming.common.pojo.dto.product.MiniProductPageQuery;
import com.liming.common.pojo.dto.product.ProductPageQuery;
import com.liming.common.pojo.dto.product.UpdateProductDTO;
import com.liming.common.pojo.dto.stock.StockPageQuery;
import com.liming.common.pojo.entity.product.Product;
import com.liming.common.pojo.vo.PageVO;
import com.liming.common.pojo.vo.product.ProductPriceVO;
import com.liming.common.pojo.vo.product.openfeign.FeignMiniProductDetailVO;
import com.liming.common.pojo.vo.product.openfeign.FeignMiniProductVO;
import com.liming.common.pojo.vo.product.openfeign.FeignProductVO;
import com.liming.common.result.LMResult;
import com.liming.product.service.ProductMircoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @author liming
 * @date 2025/1/16
 */
@Slf4j
@RestController
@AllArgsConstructor
@Tag(name = "商品接口-Service")
@RequestMapping("/service/product")
public class ProductMircoController {

    private final ProductMircoService productMircoService;

    @Schema(description = "列表")
    @GetMapping
    public LMResult<PageVO<FeignProductVO>> page(ProductPageQuery productPageQuery) {
        return LMResult.success(productMircoService.page(productPageQuery));
    }

    @Schema(description = "idProductMap - 根据名称模糊查询")
    @GetMapping("productIdsByLikeName")
    public LMResult<Map<Long, Product>> productIdsByLikeName(@RequestParam String name) {
        return LMResult.success(productMircoService.idProductMapByLikeName(name));
    }

    @Schema(description = "列表 - 根据名称模糊查询")
    @PostMapping("priceMap")
    public LMResult<Map<Long, Integer>> idAndPriceMap(@RequestBody List<Long> productIds) {
        return LMResult.success(productMircoService.idAndPriceMap(productIds));
    }

    @Schema(description = "滞销商品数量")
    @GetMapping("stagnantProductCount")
    public LMResult<List<FeignProductVO>> stagnantProductCount() {
        return LMResult.success(productMircoService.stagnantProductCount());
    }


    @Schema(description = "列表 - 根据商品IDS查询")
    @PostMapping("listByIds")
    public LMResult<List<FeignProductVO>> listByIds(@RequestBody List<Long> ids) {
        return LMResult.success(productMircoService.listProductByIds(ids));
    }

    @Schema(description = "详情")
    @GetMapping("{id}")
    public LMResult<FeignProductVO> get(@PathVariable Long id) {
        return LMResult.success(productMircoService.get(id));
    }

    @Schema(description = "查询商品价格")
    @GetMapping("price/{id}")
    public LMResult<ProductPriceVO> getPrice(@PathVariable Long id) {
        return LMResult.success(productMircoService.getPrice(id));
    }

    @Schema(description = "新增")
    @PostMapping
    public LMResult<Boolean> add(@RequestBody AddProductDTO addProductDTO) {
        return LMResult.success(productMircoService.add(addProductDTO));
    }

    @Schema(description = "导入商品")
    @PostMapping(value = "importProduct", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public LMResult<Boolean> importProduct(@RequestParam MultipartFile file) {
        return LMResult.success(productMircoService.importProduct(file));
    }

    @Schema(description = "更新")
    @PutMapping
    public LMResult<Boolean> update(@RequestBody UpdateProductDTO updateProductDTO) {
        return LMResult.success(productMircoService.updateProduct(updateProductDTO));
    }

    @Schema(description = "商品上架/下架")
    @PutMapping("{id}")
    public LMResult<Boolean> updateOnShelf(@PathVariable Long id) {
        return LMResult.success(productMircoService.updateOnShelf(id));
    }

    @Schema(description = "批量更新商品销售时间")
    @PutMapping("updateSalesTimeByIds")
    public void updateSalesTimeByIds(@RequestBody List<Long> productIds) {
        productMircoService.updateSalesTimeByIds(productIds);
    }

    @Schema(description = "删除")
    @DeleteMapping("{id}")
    public LMResult<Boolean> delete(@PathVariable Long id) {
        return LMResult.success(productMircoService.removeById(id));
    }

    @Schema(description = "批量删除")
    @DeleteMapping
    public LMResult<Boolean> deleteBatch(@RequestBody List<Long> ids) {
        return LMResult.success(productMircoService.removeBatchByIds(ids));
    }

    @Schema(description = "查询商品IDS - 库存查询参数")
    @GetMapping("idsStockQuery")
    public LMResult<List<Long>> productIdsByStockQuery(StockPageQuery query) {
        return LMResult.success(productMircoService.productIdsByStockQuery(query));
    }

    // ////////////////////////////////////////////////////////////////////////////

    @Operation(summary = "推荐商品")
    @GetMapping("recommend")
    public LMResult<List<FeignMiniProductVO>> recommend() {
        return LMResult.success(productMircoService.recommendProduct());
    }

    @Operation(summary = "商品详情 - 小程序")
    @GetMapping("detail/{id}")
    public LMResult<FeignMiniProductDetailVO> miniDetail(@PathVariable Long id) {
        return LMResult.success(productMircoService.miniDetail(id));
    }

    @Operation(summary = "商品分页 - 小程序")
    @GetMapping("miniPage")
    public LMResult<PageVO<FeignMiniProductVO>> miniPage(MiniProductPageQuery query) {
        return LMResult.success(productMircoService.miniPage(query));
    }

    @PostMapping("brandAndCategoryIdsMapByProductIds")
    public LMResult<Map<Integer, List<Long>>> brandAndCategoryIdsMapByProductIds(@RequestBody List<Long> keyIdList) {
        return LMResult.success(productMircoService.brandAndCategoryIdsMapByProductIds(keyIdList));
    }

    @PutMapping("increaseSalesByMap")
    public LMResult<Boolean> increaseSalesByMap(@RequestBody Map<Long, Integer> productIdAndQuantity) {
        return LMResult.success(productMircoService.increaseSalesByMap(productIdAndQuantity));
    }
}
